package cn.iocoder.yudao.module.reclaim.controller.admin.productcheckres;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckres.vo.ReclaimCheckResPageReqVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckres.vo.ReclaimCheckResRespVO;
import cn.iocoder.yudao.module.reclaim.controller.admin.productcheckres.vo.ReclaimCheckResSaveReqVO;
import cn.iocoder.yudao.module.reclaim.convert.CheckResConvert;
import cn.iocoder.yudao.module.reclaim.dal.dataobject.productcheckres.ReclaimCheckDeductItemDO;
import cn.iocoder.yudao.module.reclaim.dal.dataobject.productcheckres.ReclaimCheckResDO;
import cn.iocoder.yudao.module.reclaim.service.productcheckres.ReclaimCheckResService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 回收核验结果")
@RestController
@RequestMapping("/reclaim/check-res")
@Validated
public class ReclaimCheckResController {

    @Resource
    private ReclaimCheckResService productCheckResService;

    @PostMapping("/create")
    @Operation(summary = "创建回收核验结果")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:create')")
    public CommonResult<Long> createProductCheckRes(@Valid @RequestBody ReclaimCheckResSaveReqVO createReqVO) {
        return success(productCheckResService.createProductCheckRes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新回收核验结果")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:update')")
    public CommonResult<Boolean> updateProductCheckRes(@Valid @RequestBody ReclaimCheckResSaveReqVO updateReqVO) {
        productCheckResService.updateProductCheckRes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除回收核验结果")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:delete')")
    public CommonResult<Boolean> deleteProductCheckRes(@RequestParam("id") Long id) {
        productCheckResService.deleteProductCheckRes(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得回收核验结果")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:query')")
    public CommonResult<ReclaimCheckResRespVO> getProductCheckRes(@RequestParam("id") Long id) {
        ReclaimCheckResDO productCheckRes = productCheckResService.getProductCheckRes(id);
        List<ReclaimCheckDeductItemDO> itemList = productCheckResService.getProductCheckDeductItemListByResId(productCheckRes.getId());
        return success(CheckResConvert.INSTANCE.convert(productCheckRes, itemList));
    }
    @GetMapping("/get-by-orderNo")
    @Operation(summary = "获得回收核验结果")
    @Parameter(name = "orderNo", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:query')")
    public CommonResult<ReclaimCheckResRespVO> getProductCheckRes(@RequestParam("orderNo") String orderNo) {
        return success(productCheckResService.getProductCheckResByOrderNo(orderNo));
    }

    @GetMapping("/page")
    @Operation(summary = "获得回收核验结果分页")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:query')")
    public CommonResult<PageResult<ReclaimCheckResRespVO>> getProductCheckResPage(@Valid ReclaimCheckResPageReqVO pageReqVO) {
        PageResult<ReclaimCheckResDO> pageResult = productCheckResService.getProductCheckResPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReclaimCheckResRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出回收核验结果 Excel")
    @PreAuthorize("@ss.hasPermission('reclaim:product-check-res:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductCheckResExcel(@Valid ReclaimCheckResPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReclaimCheckResDO> list = productCheckResService.getProductCheckResPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "回收核验结果.xls", "数据", ReclaimCheckResRespVO.class,
                        BeanUtils.toBean(list, ReclaimCheckResRespVO.class));
    }

    // ==================== 子表（回收核验扣除项） ====================

}